package com.example.forum.interceptor;

import com.example.forum.config.AppConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Value("${forum.login.url}")
    private String DefaultUrl;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(AppConfig.getUser(request)!=null){
            return true;
        }
        if(!DefaultUrl.startsWith("/")){
            DefaultUrl="/"+DefaultUrl;
        }
        response.sendRedirect(DefaultUrl);
        return false;
    }
}
